<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" name="" id="" />
    <input type="text" name="" id="" />
    <input type="button" name="" id="" value="注册" />

    <script type="module">
      //   import axios from 'https://cdn.bootcdn.net/ajax/libs/axios/1.7.2/esm/axios.js'

      //   axios库默认导出了 axios 模块
      // import axios from "./js/axios.js";

      // let http = axios.create({
      //   baseURL: "http://localhost:8888",
      //   headers: {
      //     "content-type": "application/x-www-form-urlencoded",
      //   },
      // });

      import http from "./js/request.js";

      /* 
      
        点击注册按钮的时候，获取输入框的值，调用注册接口
      */
      let ipts = document.querySelectorAll("input");
      //获取页面打开以后的输入框的值--都是空字符串
      //   let username = ipts[0].vlaue;
      //   let password = ipts[1].vlaue;
      ipts[2].onclick = () => {
        //点击的时候再获取
        let username = ipts[0].value;
        let password = ipts[1].value;
        let url = "/users/register";
        // 标准请求方式：五种：get  post  put/patch:更新  delete:删除
        //get: { url,method,params:{搜索参数} }
        // post：{ url,method,data:{ 请求体参数 } }
        http
          .request({
            url,
            method: "post",
            data: {
              username,
              password,
              rpassword: password,
              nickname: username,
            },
          }) //返回是一个promise对象
          .then((r) => {
            // .then方法：成功并且完成之后才执行
            alert(r.data.message);
            if (r.data.code == 1) {
              //注册成功以后跳转到登录页面
              if (r.data.code == 1) {
                location.href = "02-登录.html";
              }
            }
          });
      };
    </script>
  </body>
</html>
